<?php
class Application_Model_Reward extends Application_Model_DomainObject{
    
	   protected $_id;
	
       protected $_userId;      //发起国际运单的ID

	   protected $_billNo;      //国际运单号
	
	   protected $_money;       //运单的金额
        
        protected $_rebate;      //返利的金额
        
        protected $_isAbled;      //是否有效
        
        protected $_reUserId;       //获取奖励的用户Id
        
        protected $_createTime;
        /**
         * 返利比例
         */
        const Rebates_Proportion = 0.05;
        /**
         * 无效的返利
         */
        const IsAbled_Unfinished = 1;
        /**
         * 有效的返利
         */
        const IsAbled_Complete = 2;
        /**
         * 已领取的返利
         */
        const IsAbled_Receive = 3;

        
        public function getUserItem($userId) {
            return Application_Model_User::load($userId);
        }
        public static function bybillNo($billNo) {
            $where[" billNo = "] = $billNo;
            return self::getDao()->queryOne($where);
        }
        public static function getByFilter($quick, $startTime, $entTime, $isAbled, $isComp,$pagesize, $pageindex) 
        {
            $where = array();
            $and = "";
            if($quick != null) {
                $userItem = Application_Model_User::getByNikeNameStore($quick);
                if($userItem){
                    $arrUserId = "(";
                    foreach ($userItem as $userVal) {
                        $arrUserId .= $userVal->Id.",";
                    }
                    $arrUserId .= "0)";
                    $where[$and." ( userId in "] = $arrUserId;
                    $and = " or ";
                    $where[$and." reUserId in "] = $arrUserId;
                    $where[" ) and 1 = "] = 1;
                    $and = " and  ";
                }
            }
            if ($startTime != null) {
                $where[$and." createTime >= "] = $startTime;
                $and = " and ";
            }
            if ($entTime != null) { 
                $where[$and." createTime <= "] = $entTime." 23:59:59";
                $and = " and ";
            }
            if ($isAbled != null) {
                $where[$and." isAbled = "] = $isAbled;
                $and = " and ";
            }
            if($isComp == self::IsAbled_Unfinished){
                $where[$and." isAbled = "] = self::IsAbled_Unfinished;
                $and = " and ";
            } else if ($isComp == self::IsAbled_Complete) {
                $where[$and." isAbled in "] = "(".self::IsAbled_Complete.",".self::IsAbled_Receive.")";
                $and = " and ";
            }
            return self::getDao()->queryPage($where, " id desc ", $pagesize, $pageindex);
        }     
        
        
        public static function CountByUserIdIsAbled($userId,$isAbled) {
            $where[" userId = "] = $userId;
            if($isAbled == self::IsAbled_Unfinished){
                $where[" and isAbled = "] = self::IsAbled_Unfinished;
            } else {
                $where[" and isAbled in "] = "(".self::IsAbled_Complete.",".self::IsAbled_Receive.")";
            }
            return self::getDao()->count($where);
        }
        
        public static function SumByUserIdIsAbled($userId,$isAbled) {
            $where[" userId = "] = $userId;
            if($isAbled == self::IsAbled_Unfinished){
                $where[" and isAbled = "] = self::IsAbled_Unfinished;
            } else {
                $where[" and isAbled in "] = "(".self::IsAbled_Complete.",".self::IsAbled_Receive.")";
            }
            return self::getDao()->sum($where, "rebate");
        }
        
        public static function CountByUserId($userId) {
            return self::getDao()->count(array("userId = "=>$userId));
        }
        
        public static function getFiltel($userId,$reUserId,$pagesize,$pageindex) 
        {
            $where = array();
            $and = "";
            if($userId != null) {
                $where[$and." userId = "] = $userId;
                $and = " and ";
            }
            if($reUserId != null) {
                $where[$and." reUserId = "] = $reUserId;
                $and = " and ";
            }
            return self::getDao()->queryPage($where, " id desc ", $pagesize,$pageindex);
        }
        
        public static function Receive($id,$userId)
        {
            $rewardItem = self::load($id);
            if($rewardItem && $rewardItem->ReUserId == $userId){
                if($rewardItem->IsAbled == self::IsAbled_Complete){
                    $accountItem = Application_Model_UserAccount::addAccount($userId);
                    $accountItem->changeMoney($rewardItem->Rebate, Application_Model_UserAccountHistory::TYPE_REBATE, "国际运单".$rewardItem->BillNo."返利");
                    $rewardItem->IsAbled = self::IsAbled_Receive;
                    $rewardItem->save();
                    return 1;
                }
            }
            return 0;
        }

        public static function ByIsAbled($isAbled) {
            return self::getDao()->queryAll(array("isAbled = " => $isAbled));
        }

        public static function ByIsAbledAll() {
            return array(
                self::IsAbled_Unfinished => "不可用"
                , self::IsAbled_Complete => "可领取"
                , self::IsAbled_Receive => "已领取"
            );
        }
        
        public function getIsAbledName()
        {
            $isAbledAll = self::ByIsAbledAll();
            return $isAbledAll[$this->_isAbled];
        }
        
        public function getWayInfo()
        {
            return Application_Model_wayBillItem::bybillNo($this->_billNo);
        }

}